package logic

import (
	"context"
	"strconv"
	"user_srv/pkg"
	"user_srv/user"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginLogic) Login(req *types.LoginReq) (resp *types.Resp, err error) {
	// todo: add your logic here and delete this line
	login, err := l.svcCtx.UserSrv.Login(l.ctx, &user.LoginRequest{
		Phone:       req.Phone,
		Password:    req.Password,
		SendSmsCode: req.SendSmsCode,
	})
	handler, err := pkg.TokenHandler(strconv.FormatInt(login.Id, 10))

	if err != nil {
		return &types.Resp{
			Mes:  err.Error(),
			Code: 500,
			Data: nil,
		}, nil
	}

	return &types.Resp{
		Mes:  "登陆一体化成功",
		Code: 200,
		Data: handler,
	}, nil
}
